package com.atguigu.crowd.mvc.controller;

import com.atguigu.crowd.entity.Menu;
import com.atguigu.crowd.service.api.MenuService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zzqstart
 * @Description
 * @date 2022-09-05 12:41
 */
//@Controller
@RestController
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("assign/get/assigned/menu/id/by/role/id.json")
    public ResultEntity<List<Integer>> queryInnerMenuAuth(@RequestParam("menuId") Integer menuId){
        List<Integer> authIdList =  menuService.queryInnerMenuAuth(menuId);
        return ResultEntity.successWithData(authIdList);
    }


        @RequestMapping("assign/do/menu/assign/auth.json")
    public ResultEntity<String> assginAuthToMeun(@RequestBody Map<String,List<Integer>> map) {
        menuService.assginAuthToMeun(map);
        return ResultEntity.successWithoutData();
    }


        // @ResponseBody
    @RequestMapping("menu/remove.json")
    public ResultEntity<String> remove(Integer id) {
        menuService.remove(id);
        return ResultEntity.successWithoutData();

    }

    // @ResponseBody
    @RequestMapping("/menu/edit.json")
    public ResultEntity<String> edit(Menu menu) {
        menuService.edit(menu);
        return ResultEntity.successWithoutData();
    }

    // @ResponseBody
    @RequestMapping("/menu/save.json")
    public ResultEntity<String> save(Menu menu) {
        menuService.add(menu);
        return ResultEntity.successWithoutData();
    }

    // @ResponseBody
    @RequestMapping("/menu/get/whole/tree.json")
    public ResultEntity<Menu> getWholeTree() {

        // 1 查询全部Menu对象
        List<Menu> menuList = menuService.getAll();
        // 2 存储根节点
        Menu root = null;

        // 3 创建map对象用来存储id和mnue对象对应关系便于查找父节点
        Map<Integer, Menu> map = new HashMap<>();

        // 4 填充map
        for (Menu menu : menuList) {
            map.put(menu.getId(), menu);
        }
        // 5 查找根节点，组装父子节点
        for (Menu menu : menuList) {
            Integer pid = menu.getPid();
            if (pid == null) {
                root = menu;
                continue;
            }

            if (map.size() != 0 && map != null && map.containsKey(pid)) {
                Menu parentMenu = map.get(pid);
                parentMenu.getChildren().add(menu);
            }
        }
        return ResultEntity.successWithData(root);
    }


    public ResultEntity<Menu> getWholeTreeOleMethor() {
        // 1 查询全部Menu对象
        List<Menu> menuList = menuService.getAll();
        // 2 存储根节点
        Menu root = null;

        for (Menu menu : menuList) {
            // 找到根节点
            if (menu.getPid() == null) {
                root = menu;
                // 结束本次循环
                continue;
            }
            for (Menu maybeParent : menuList) {
                // 寻找当前节点的父节点
                if (Objects.equals(menu.getPid(), maybeParent.getPid())) {
                    maybeParent.getChildren().add(menu);
                    break;
                }
            }

        }
        return ResultEntity.successWithData(root);
    }

}
